package com.xdy.mdc.util;

import android.view.LayoutInflater;

import androidx.viewbinding.ViewBinding;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;

/**
 * @Author: XDY.
 * @Email: 2280944114@qq.com
 * @CreateTime: 2023-08-27  21:09
 * @Description: 反射工具类
 * @Version: 1.0
 */
public class ReflectUtil {
    public static <VB extends ViewBinding> VB newViewBinding(LayoutInflater layoutInflater, Class<?> clazz){
        ParameterizedType type = null;
        try {
            type = (ParameterizedType) clazz.getGenericSuperclass();
        }catch (Exception e){
            type = (ParameterizedType) clazz.getSuperclass().getGenericSuperclass();
        }
        Class<VB> obj = (Class<VB>) type.getActualTypeArguments()[0];
        try {
            Method method = obj.getMethod("inflate", LayoutInflater.class);
            return (VB) method.invoke(null,layoutInflater);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}